import { validatePass, validatePass2, validateUsername } from '../fn/validate';

describe('validatePass function test', () => {
  test('should return error message for empty password', () => {
    const rule = {};
    const value = '';
    const callback = jest.fn();

    validatePass(rule, value, callback);

    expect(callback).toHaveBeenCalledWith(new Error('请输入密码'));
  });

  test('should return error message for password less than 5 characters', () => {
    const rule = {};
    const value = '1234';
    const callback = jest.fn();

    validatePass(rule, value, callback);

    expect(callback).toHaveBeenCalledWith(new Error('密码不能小于5位'));
  });

  test('should return no error message for valid password', () => {
    const rule = {};
    const value = 'password123';
    const callback = jest.fn();

    validatePass(rule, value, callback);

    expect(callback).toHaveBeenCalledWith();
  });
});

describe('validatePass2 function test', () => {
  it('should return an error message if value is empty', () => {
    const rule = {};
    const value = '';
    const callback = jest.fn();

    validatePass2(rule, value, callback);

    expect(callback).toHaveBeenCalledWith(new Error('请输入新密码'));
  });

  it('should return an error message if value length is less than 5', () => {
    const rule = {};
    const value = 'pass';
    const callback = jest.fn();

    validatePass2(rule, value, callback);

    expect(callback).toHaveBeenCalledWith(new Error('密码不能小于5位'));
  });

  it('should not return an error message if value is valid', () => {
    const rule = {};
    const value = 'password';
    const callback = jest.fn();

    validatePass2(rule, value, callback);

    expect(callback).toHaveBeenCalledWith();
  });
});

describe('validateUsername function test', () => {
  it('should return an error message if value is empty or only contains whitespaces', () => {
    const rule = {};
    const emptyValue = '';
    const whitespaceValue = '     ';
    const callback = jest.fn();

    // Test with empty value
    validateUsername(rule, emptyValue, callback);
    expect(callback).toHaveBeenCalledWith(new Error('请输入账号'));

    // Test with value containing only whitespaces
    validateUsername(rule, whitespaceValue, callback);
    expect(callback).toHaveBeenCalledWith(new Error('请输入账号'));
  });

  it('should not return an error message if value is valid', () => {
    const rule = {};
    const validValue = 'username';
    const callback = jest.fn();

    validateUsername(rule, validValue, callback);
    expect(callback).toHaveBeenCalledWith();
  });
});